﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Threading;

namespace Hip.Define_Test {
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow: Window, INotifyPropertyChanged {
		public MainWindow() {
			InitializeComponent();
			//testViewer.DataContext = item1;
			this.Closing += delegate {
				Properties.Settings.Default.Save();
			};
		}

		private double _progress;
		public double Progress {
			get {
				return _progress;
			}
			set {
				if ( _progress != value ) {
					_progress = value;
					Changed( "Progress" );
				}
			}
		}

		private Hip.Define_Test.TestModel _item1;
		public Hip.Define_Test.TestModel item1 {
			get {
				if ( _item1 == null ) {
					_item1 = new TestModel { Name = "Logan", Address = "110 Princess St", Awesome = true };
				}
				return _item1;
			}
			//set {
			//   if ( _item1 != value ) {
			//      _item1 = value;
			//      Changed( "item1" );
			//   }
			//}
		}
		private Hip.Define_Test.TestModel _item2;
		public Hip.Define_Test.TestModel item2 {
			get {
				if ( _item2 == null ) {
					_item2 = new TestModel { Name = "Dude Meister", Address = "Nowhere Lane", Awesome = false };
				}
				return _item2;
			}
			//set {
			//   if ( _item1 != value ) {
			//      _item1 = value;
			//      Changed( "item1" );
			//   }
			//}
		}

		protected virtual void Changed( string propertyName ) {
			PropertyChangedEventHandler handler = PropertyChanged;
			if ( handler != null ) {
				handler( this, new PropertyChangedEventArgs( propertyName ) );
			}
		}

		private bool _magic = false;
		public bool Magic {
			get {
				return _magic;
			}
			set {
				if ( _magic != value ) {
					_magic = value;
					Changed( "Magic" );
				}
			}
		}

		[DependsOn( "this.Magic" )]
		public string MagicButtonLabel {
			get {
				if ( Magic )
					return "Turn off the magic. :(";
				else
					return "Enable magic!";
			}
		}

		[DependsOn( "this.Magic" )]
		private string GetSampleText() {
			if ( Magic )
				return "Magic is enabled! (It's just pretend though, it doesn't actually do anything.)";
			else
				return "No magic yet...";
		}

		public string[] MenuItems {
			get {
				return new string[] { "one", "two", "three" };
			}
		}

		public event PropertyChangedEventHandler PropertyChanged;

		private void MagicButton_Click( object sender, RoutedEventArgs e ) {
			Magic = !Magic;
		}

		private void ChangeDataContext_Click( object sender, RoutedEventArgs e ) {
			if ( testViewer.DataContext == item1 )
				testViewer.DataContext = item2;
			else
				testViewer.DataContext = item1;
		}

		private void StartOperation_Click( object sender, RoutedEventArgs e ) {
			ThreadPool.QueueUserWorkItem( new WaitCallback( delegate( object target ) {
				for ( int i = 0; i < 60; i++ ) {
					Thread.Sleep( 100 );
					Progress = (double)i / 59.0;
				}
			} ) );
		}
	}
}
